#include "configanalyze.h"
#include <QSettings>

ConfigAnalyze::ConfigAnalyze()
{
    key_value_map_.clear();
    json_to_map_.clear();
    json_object_list_.clear();
    key_value_.clear();
    second_key_.clear();
}

QString ConfigAnalyze::getFileName(QString path)
{
    QFileInfo fileInfo = QFileInfo(path);
    QString fileName = fileInfo.fileName();
    return fileName;
}

void ConfigAnalyze::convertJsonToMap(QString path, QJsonObject o, int level)
{
    for(int i = 0; i < o.keys().size(); i++)
    {
        QString path_temp = path;
        path_temp.append("/").append(o.keys()[i]);
        if(o.value(o.keys()[i]).isObject())
            convertJsonToMap(path_temp, o.value(o.keys()[i]).toObject(), level+1);
        if(o.value(o.keys()[i]).isString() || o.value(o.keys()[i]).isArray())
            key_value_map_.insert(path_temp,o.value(o.keys()[i]).toString());
    }
}

void ConfigAnalyze::jsonToMap()
{
    QMap <QString,QMap<QString,QString>> first_layer_map;
    QMap <QString,QString> second_layer_map;
    QString first_layer_key;
    QString second_layer_key_temp;
    QString second_layer_value_temp;
    for(int i = 0; i < json_object_list_.size(); i++)
    {
        key_value_map_.clear();
        first_layer_map.clear();
        second_layer_map.clear();
        first_layer_key = json_object_list_[i].keys()[0];
        if(json_object_list_[i].value(first_layer_key).isArray())
        {
            QJsonArray array = json_object_list_[i].value(first_layer_key).toArray();
            for(int i = 0; i < array.size(); i++)
            {
                second_layer_key_temp = QString("/").append(array[i].toString());
                second_layer_value_temp= "";
                second_layer_map.insert(second_layer_key_temp, second_layer_value_temp);
            }
        }
        else
        {
            convertJsonToMap("",json_object_list_[i], 0);
            for(auto iterator  = key_value_map_.begin(); iterator != key_value_map_.end(); iterator++)
            {
                second_layer_key_temp = iterator.key().split(QString("/").append(first_layer_key))[1];
                second_layer_value_temp = iterator.value();
                second_layer_map.insert(second_layer_key_temp, second_layer_value_temp);
            }
        }
        first_layer_map.insert(first_layer_key,second_layer_map);
        //qDebug()<<first_layer_map;
        json_to_map_.push_back(first_layer_map);
    }
    setSectionIndexValue();
    setKeyIndexvalue();
}

void ConfigAnalyze::setTable(SqlRecordCountTableModel *table)
{
    table_model_ = table;
}

void ConfigAnalyze::setCombobox(ComboBoxModel *combobox_section_model,ComboBoxModel *combobox_key_model)
{
    combobox_section_model_ = combobox_section_model;
    combobox_key_model_ = combobox_key_model;
}


void ConfigAnalyze::splitJsonFile()
{

    QStringList keys = json_object_.keys();
    for(int i = 0; i < keys.size(); i++)
    {
        if(keys.at(i)[0] == "X" && keys.at(i)[1] == "m" && keys.at(i)[2] == "l")
        {
            QString key = keys.at(i);
            QJsonObject value = json_object_.value(json_object_.keys()[i]).toObject();
            QJsonObject item;
            item.insert(key,value);
            json_object_list_.push_back(item);
        }
    }
    jsonToMap();

}


void  ConfigAnalyze::insertKeyAndValue(int index)
{
    current_section_index_ = index;
    setKeyIndexvalue();
    QMap <QString,QMap<QString,QString> > big_map = json_to_map_[index];
    QMap<QString,QString> small_map = big_map.values()[0];
    table_model_->clearRows();
    qDebug()<<"size:"<<small_map.size();
    for(int j = 0; j < small_map.size(); j++){
        table_model_->appendRow("1",(QVariantList()<<small_map.keys()[j]<<small_map.value(small_map.keys()[j])));
    }
}

void ConfigAnalyze::analyzFile(QString path)
{
    QFile file(path);
    if (! file.open(QIODevice::ReadOnly))
        qDebug()<<"文件打开失败。";
    QByteArray data = file.readAll();
    QJsonParseError jsonError;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
    if (jsonError.error != QJsonParseError::NoError)
    {
        qDebug()<<jsonError.errorString();
    }
    else if (jsonDoc.isObject())
    {
        //------保存jsonObject
        json_object_ = jsonDoc.object();
        splitJsonFile();
    }
    else if (jsonDoc.isArray())
    {
        QJsonArray jsonArray = jsonDoc.array();
    }
    else
    {
        qDebug()<<"Json文件解析为空。";
    }
    file.close();

}

void ConfigAnalyze::setSectionIndexValue()
{
    QString section_temp;
    QList<QString> key_value;
    key_value.clear();
    for(int i = 0; i < json_to_map_.size(); i++)
    {
        for(auto iterator  = json_to_map_[i].begin(); iterator != json_to_map_[i].end(); iterator++)
        {
            section_temp.clear();
            section_temp.append(iterator.key());
            key_value.push_back(section_temp);
        }
    }
    key_value_ = key_value;
    combobox_section_model_->setComboList(key_value);
}

void ConfigAnalyze::setKeyIndexvalue()
{
    QList<QString> key_value;
    key_value.clear();
    QMap <QString,QMap<QString,QString> > big_map = json_to_map_[current_section_index_];
    QMap<QString,QString> small_map = big_map.values()[0];

    for(int j = 0; j < small_map.size(); j++){
        key_value.push_back(small_map.keys()[j]);
    }
    second_key_ = key_value;
    combobox_key_model_->setComboList(key_value);
}




//void ConfigAnalyze::writeJson()
//{
//    QFile file("../1.json");
//    if(!file.open(QIODevice::ReadWrite)) {
//        qDebug() << "File open error";
//    } else {
//        qDebug() <<"File open!";
//    }
//    QJsonDocument jsonDoc;
//    QJsonObject json_temp;
//    json_temp.insert(json_object_list_[0].keys()[0],json_object_list_[0].value(json_object_list_[0].keys()[0]));
//    json_temp.insert(json_object_list_[1].keys()[0],json_object_list_[1].value(json_object_list_[1].keys()[0]));
//    jsonDoc.setObject(json_temp);
//    file.write(jsonDoc.toJson());
//    file.close();
//    qDebug() << "Write to file";
//}

void initalize(QStringList path)
{

}


void ConfigAnalyze::writeJson()
{
    QList<QStringList> path;
//    QString path = json_to_map_[0].values()[0].keys()[0];
//    qDebug()<<path;
//    QString value  = json_to_map_[0].values()[0].values()[0];
//    qDebug()<<value;
//    QStringList key = path.split("/");
//    qDebug()<<key;
//    qDebug()<<json_to_map_;
    for(int i = 0; i < json_to_map_[0].values()[0].size(); i++)
    {
        //qDebug()<<"size:"<<json_to_map_[0].values()[0].size();

        QString path_temp = json_to_map_[0].values()[0].keys()[i];
        qDebug()<<path_temp;
        QStringList key = path_temp.split("/");
        qDebug()<<key;
        path.push_back(key);
    }

    QJsonObject root;

    for(int i = 0; i < path.size(); i++)
    {
        if(root.contains(json_to_map_[0].keys()[0]));
            initalize(path[i]);
        for(int j = 1; j < path[i].size(); j++)
        {








        }
    }

//    qDebug()<<path;
}


void ConfigAnalyze::addField(QString key, QString value)
{
    QMap<QString,QString> map_temp = json_to_map_[current_section_index_].values()[0];
    map_temp.insert(key,value);

    json_to_map_[current_section_index_].remove(key_value_[current_section_index_]);
    json_to_map_[current_section_index_].insert(key_value_[current_section_index_],map_temp);

    insertKeyAndValue(current_section_index_);
}



void ConfigAnalyze::deleteConfig(int index)
{
    QMap<QString,QString> map_temp = json_to_map_[current_section_index_].values()[0];
    map_temp.remove(second_key_[index]);
    json_to_map_[current_section_index_].remove(key_value_[current_section_index_]);
    json_to_map_[current_section_index_].insert(key_value_[current_section_index_],map_temp);
    insertKeyAndValue(current_section_index_);
}

void ConfigAnalyze::reviseConfig(int index, QString key, QString value)
{
    deleteConfig(index);
    addField(key,value);
    insertKeyAndValue(current_section_index_);
}

